001    /* $RCSfile: BigIntegerUtil.java,v $
002     * $Revision: 1.2 $
003     * $Date: 2002/01/04 14:05:50 $
004     * $Author: uwe $
005     * $State: Exp $
006     *
007     * Created on December 20, 2001 10:13 AM
008     *
009     * Copyright (C) 2001 Uwe Guenther <uwe@cscc.de>
010     *
011     * This file is part of the jhbci JCE-ServiceProvider. The jhbci JCE-
012     * ServiceProvider is a library, written in JavaTM, that should be 
013     * used in HBCI banking applications (clients and may be servers),
014     * to do cryptographic operations.
015     *
016     * The jhbci library is free software; you can redistribute it and/or
017     * modify it under the terms of the GNU Lesser General Public
018     * License as published by the Free Software Foundation; either
019     * version 2.1 of the License, or (at your option) any later version.
020     *
021     * The jhbci library is distributed in the hope that it will be useful,
022     * but WITHOUT ANY WARRANTY; without even the implied warranty of
023     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
024     * Lesser General Public License for more details.
025     *
026     * You should have received a copy of the GNU Lesser General Public
027     * License along with this library; if not, write to the Free Software
028     * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
029     *
030     */
031    
032    package de.cscc.crypto.util;
033    
034    import java.math.BigInteger;
035    
036    /** 
037     * BigIntegerUtil Class.
038     *
039     * <p>This is a wrapper class for frequentliy used methods that are missed in 
040     * the BigInteger class. Because BigInteger is immutable we are not able to
041     * extend this class.
042     *
043     * @author  <a href=mailto:uwe@cscc.de>Uwe Günther</a>
044     *
045     * @version $Revision: 1.2 $
046     */
047    public final class BigIntegerUtil {
048    
049        /** This class can't be instance, because its constructor is private.*/
050        private BigIntegerUtil() {}
051    
052        /**
053         * Cuts a leading 0x00 byte that indicates that this number is non negative.
054         *
055         * <p>BigInterger#toByteArray returns a two's-complement representation
056         * of the BigInteger. This does not matter because we hav a non negative
057         * number (number >= 0). Two's Complement uses the leftmost bit (most
058         * significant bit) for the sign bit. Because BigInteger represents
059         * infinite numbers, there is no concrete sign bit. This isn't realy a
060         * problem for us, because positive numbers has no sign bit. But what, if
061         * a positive number where the leftmost bit in its leftmost byte is set?
062         * Is it a negative or a positive number? For these cases
063         * BigInteger#toByteArray adds a whole 0x00 byte add the left side to
064         * indicate this number is positive. This byte is bad for us, so we want to
065         * filter it out in this method.
066         *
067         * @param signature the signature that will be converted to an unsigned byte[].
068         * @throws NullPointerException if signature is null.
069         * @throws IllegalArgumentException if signature is a negative number.
070         * @see java.math.BigInteger#toByteArray
071         */
072        public static byte[] toUnsignedByteArray(BigInteger signature) {
073            
074            if (signature == null) {
075                throw new NullPointerException("Parameter signature is null.");
076            }
077            if (signature.compareTo(BigInteger.ZERO) < 0) {
078                //signature < ZERO
079                throw new IllegalArgumentException(
080                "Parameter signature is negative. signature: " + signature);
081            }
082            
083            //Fetch two's-complement representation of the signature (BigInteger)
084            byte[] twosComplement = signature.toByteArray();
085            
086            //If there is no leading Zero byte, we are right.
087            if (twosComplement[0] != 0x00) {
088                return twosComplement;
089            }
090            //We have a leading 0x00 byte, but twosComplement.length is only 1, that
091            //means our signature has the number "0".
092            if (twosComplement.length == 1) {
093                return twosComplement;
094            }
095            //If we are at this point, we should cut the leading 0x00 byte that
096            //is only an indicator that our number is not negative.
097            byte[] unsigned = new byte[twosComplement.length - 1];
098            for (int i = 0; i < unsigned.length; i++) {
099                unsigned[i] = twosComplement[i + 1];
100            }
101            //We return the new unsigned value.
102            return unsigned;
103        }            
104        
105    
106    }